package algotithm.leetcode.listnode.test817;

import algotithm.leetcode.listnode.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/22-22:43
 * @URL https://leetcode-cn.com/problems/linked-list-components/
 * @Title 817. 链表组件
 */
class Solution {

    public int numComponents(ListNode head, int[] G) {
        Set<Integer> Gset = new HashSet();
        for (int x: G) {
            Gset.add(x);
        }
        ListNode cur = head;
        int ans = 0;

        while (cur != null) {
            if (Gset.contains(cur.val) && (cur.next == null || !Gset.contains(cur.next.val))) {
                ans++;
            }
            cur = cur.next;
        }

        return ans;
    }
}


